package util

import (
	"github.com/sirupsen/logrus"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"time"
)

func IsDir(paths string) bool {
	f, err := os.Stat(paths)
	if err != nil {
		return false
	}
	return f.IsDir()
}
func IsEmptyDir(path string) bool {
	f, err := ioutil.ReadDir(path)
	if err != nil {
		return false
	}
	return len(f) == 0
}

// GetFileList 得出当前目录内容
func GetFileList(paths string, fileMap map[string]*MyFileInfo) (lst []string) {
	f, err := ioutil.ReadDir(paths)
	if err != nil {
		return
	}
	var myFiles []MyFileInfo = make([]MyFileInfo, len(f))
	for i, file := range f {
		myFiles[i] = MyFileInfo{
			File: file, Size: fileMap[paths+"/"+file.Name()].Size,
		}
	}

	sort.Sort(FileSort(myFiles))

	for _, file := range myFiles {
		path1 := paths + string(filepath.Separator) + file.File.Name()
		lst = append(lst, path1)
	}
	return
}

func FileCopy(src string, destDir string) {
	destFilePath := destDir + "/" + GetFileName(src)
	_, err := os.Stat(destFilePath)
	if err == nil {
		destFilePath = destDir + "/" + GetFilePrefixName(src) + "_" + strconv.FormatInt(time.Now().UnixMicro(), 10) + GetFileSuffixName(src)
	}
	source, err := os.Open(src)
	if err != nil {
		logrus.Error("打开文件" + src + "失败:" + err.Error())
		return
	}
	defer source.Close()

	destination, err := os.Create(destFilePath)
	if err != nil {
		logrus.Error("创建文件" + destFilePath + "失败:" + err.Error())
		return
	}
	defer destination.Close()
	bytes, err := io.Copy(destination, source)
	if err != nil {
		logrus.Error("复制文件" + src + "到" + destFilePath + "失败:" + err.Error())
		return
	}
	logrus.Info("复制文件" + src + "到" + destFilePath + "成功," + strconv.FormatInt(bytes, 10))
}
func GetFileSuffixName(path string) string {
	return filepath.Ext(path)
}
func GetFilePrefixName(path string) string {
	fileName := GetFileName(path)
	return strings.TrimSuffix(fileName, GetFileSuffixName(path))
}

func GetFileName(path string) string {
	file, err := os.Stat(path)
	if err != nil {
		return "--"
	} else {
		return file.Name()
	}

}

func TruncDecimal(value float64, size int) string {
	return strconv.FormatFloat(value, 'f', size, 64)
}
func FormatBytes(size int64) string {
	if size < 1024 {
		//B
		return strconv.FormatInt(size, 10) + "B"
	} else if (size >= 1024) && (size < (1024 * 1024)) {
		//kb
		return TruncDecimal(float64(size)/(1024.0), 2) + "KB"
	} else if (size >= 1024*1024) && (size < (1024 * 1024 * 1024.0)) {
		//MB
		return TruncDecimal(float64(size)/(1024.0*1024.0), 2) + "MB"
	} else if size >= 1024*1024*1024 {
		//GB
		return TruncDecimal(float64(size)/(1024.0*1024.0*1024.0), 2) + "GB"
	}
	return strconv.FormatInt(size, 10) + "B"
}
func CalcDirSize(dir string, level int, fileMap map[string]*MyFileInfo) int64 {
	file, err := os.Stat(dir)

	fileMap[dir] = &MyFileInfo{File: file, Level: level, Path: dir, Size: 0}

	files, err := ioutil.ReadDir(dir)
	if err != nil {
		return 0
	}

	var size int64 = 0
	for _, file := range files {
		var filePath string = dir + "/" + file.Name()
		if file.IsDir() {
			size += CalcDirSize(filePath, level+1, fileMap)
		} else {
			fileMap[filePath] = &MyFileInfo{File: file, Level: level + 1, Path: filePath, Size: file.Size()}
			size += file.Size()
		}
	}
	fileInfo := fileMap[dir]
	fileInfo.Size = size
	return size
}
func DeleteFile(path string) {
	err := os.Remove(path)
	if err != nil {
		logrus.Error("删除失败:" + path + ",原因:" + err.Error())
	} else {
		logrus.Info("删除成功:" + path)
	}
}
